package com.xym.manage.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.abel533.mapper.Mapper;
import com.xym.manage.mapper.ContentCategoryMapper;
import com.xym.manage.mapper.ContentMapper;
import com.xym.manage.pojo.Content;
import com.xym.manage.pojo.ContentCategory;

@Service
public class ContentCategoryService extends BaseService<ContentCategory>{
	
	@Autowired
	private ContentCategoryMapper  contentCategoryMapper;
	
	@Autowired
	private ContentMapper contentMapper;
	
	@Override
	public Mapper<ContentCategory> getMapper() {
		
		return this.contentCategoryMapper;
	}

	public List<ContentCategory> queryContentCategory(Long pid) {
		
		return contentCategoryMapper.select(null);
	}

	/**
	 * 新增内容分类节点
	 * @param contentCategory
	 * @return 
	 */
	public Boolean saveContentCategory(ContentCategory contentCategory) {
		
		//查询父节点对象
		/*ContentCategory parent = contentCategoryMapper.selectByPrimaryKey(contentCategory.getParentId());
		if(!parent.getIsParent()){
			parent.setIsParent(true);
			contentCategoryMapper.updateByPrimaryKey(parent);
		}*/
		
		//查询父节点优化
		ContentCategory contentCategory2 = new ContentCategory();
		contentCategory2.setParentId(contentCategory.getParentId());
		int j = contentCategoryMapper.selectCount(contentCategory2);
		
		if(j==0){
			ContentCategory parent = new ContentCategory();
			parent.setIsParent(true);
			parent.setId(contentCategory.getParentId());
			contentCategoryMapper.updateByPrimaryKeySelective(parent);
		}
		
		contentCategory.setId(null);
		contentCategory.setSortOrder(1);
		contentCategory.setIsParent(false);
		contentCategory.setStatus(1);
		int i = this.save(contentCategory);
		
		if(i>0){
			return true;
		}
		
		return false;
	}

	/**
	 * 删除内容节点和它的所有子节点
	 * @param id
	 * @param parentId
	 * @return
	 */
	public Boolean deleteContentCategory(Long id, Long parentId) {
		
		ContentCategory category = new ContentCategory();
		category.setParentId(parentId);
		//修改父节点属性
		if(parentId!=0){
			//查找父节点下的子节点,等于1表示只有当前子节点
			Integer count = this.queryCount(category);
			if(count==1){
				ContentCategory parent = new ContentCategory();
				parent.setId(parentId);
				parent.setIsParent(false);
				this.updateSelective(parent);
			}
		}
		
		
		ArrayList<Object> list = new ArrayList<Object>();
		//递归查找
		queryIds(list,id);
		Integer i = this.deleteByIds("id", list, ContentCategory.class);
		
		//如果分类下有内容也要删除
		contentMapper.deleteByIds(list);
		
		if(i>0){
			return true;
		}
		
		return false;
	}

	

	/**
	 * 递归查询 删除的节点和它的子节点
	 * @param ids
	 * @param id
	 */
	private void queryIds(ArrayList<Object> ids, Long id) {
		ids.add(id);
		
		ContentCategory category = new ContentCategory();
		category.setParentId(id);
		List<ContentCategory> list = contentCategoryMapper.select(category);
		if(!CollectionUtils.isEmpty(list)){
			for (ContentCategory contentCategory : list) {
				queryIds(ids, contentCategory.getId());
			}
		}
	}

}
